export const setKey = (data: any[] | object, field = 'key') => {
    if (Array.isArray(data)) {
        return data?.map((item: any, index: number) => ({
            ...item,
            [field]: randomString(8)
        }))
    }
    return data;
}

/**
 * 获取唯一标识
 * @param len 指定长度
 * @returns 
 */
export const randomString = (len: number = 32) => {
    var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz1234567890'
    var maxPos = chars.length
    var pwd = ''
    for (var i = 0; i < len; i++) {
        pwd += chars.charAt(Math.floor(Math.random() * maxPos))
    }
    return pwd
}
